// pages/index/detail/activity-detail.js

var WxParse = require('../../../wxParse/wxParse.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl:app.globalData.imgurl,
    disabled: 0,  // 按钮是否disable
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this
    wx.showLoading({
      title: 'Loading',
      mask: true,
    })
    that.geDetail(e)
  },
  // 关注
  guanzhu:function(e){
    var o = e.currentTarget.dataset
    var that = this
    wx.request({
      url: app.globalData.url + 'Index/followhd?hd_id=' + o.id+ "&status="+ ((this.data.follow+1) % 2) + '&wxopenid=' + wx.getStorageSync('openId'),

      success:function(res){
        that.geDetail({id:o.id})
      }
    })
    this.setData({
      follow:!this.data.follow
    })
  },
  geDetail:function(e){
    var that = this
    wx.request({
      url: app.globalData.url + 'Index/detail?hd_id=' + e.id + '&wxopenid=' + wx.getStorageSync('openId'),
      success: function (res) {
        var ac_info = res.data.data
        var userlist = res.data.data2 ? res.data.data2:[]
        ac_info.hd_tags = ac_info.hd_tags.split(',')
        var art = ac_info.content;
        WxParse.wxParse('art', 'html', art, that, 5);
        that.setData({
          follow: res.data.follow, // 关注
          baoming: res.data.baoming, // 报名
          user:res.data.user,
          ac_info: ac_info, // 活动信息
          starttime: that.getLocalTime(ac_info.hd_starttime), // 活动时间
          endtime: that.getLocalTime(ac_info.hd_endtime), // 活动时间
          userlist: userlist, //报名人员列表
        })
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  },
  getLocalTime: function (nS) {
    // 时间戳处理
    // donghong 2017-8-21
    var _date = new Date(parseInt(nS) * 1000).toLocaleString().substr(0, 10).split('/')
    if (_date[1].length == 1) {
      _date[1] = '0' + _date[1]
    }
    if (_date[2].length == 3) {
      _date[2] = _date[2].substr(0, 2)
    }
    var date = _date.join('-')
    return date
  },
  // 是否可以报名
  isbaoming:function(e){
    var o = e.currentTarget.dataset
    var that = this
    if(this.data.user){
      this.setData({
        showUser:true
      })
      return
    }
     this.setData({
       iswxzf:true
     })
  },
  //报名
  baoming:function(){
    var that = this
    wx.request({
      url:app.globalData.url+ 'Index/baominghd',
      data: {
        wxopenid: wx.getStorageSync('openId'),
        hd_id: that.data.ac_info.id,
        money: that.data.ac_info.hd_money
      },
      success: function(res) {
        console.log(res)
        wx.request({
          url: app.globalData.url +'Pay',
          data:{
            wxopenid: wx.getStorageSync('openId'),
            order_id:res.data.order_id
          },
          success:function(res){
            console.log(res)
            var res = res.data
            wx.requestPayment({
              timeStamp: res.timeStamp,
              nonceStr: res.nonceStr,
              package: res.package,
              signType: res.signType,
              paySign: res.paySign,
              success:function(res){
                that.setData({
                  success:true,
                  baoming:0
                })
                //that.geDetail({ id: that.data.ac_info.id })  // 报名成功刷新页面

              },
              fail:()=>{
                wx.showModal({
                  title: '提示',
                  content: '支付取消',
                  showCancel: false,
                  confirmColor: '#ffbf00',
                })
              },
              complete:()=>{
                that.setData({
                  iswxzf: false
                })
              }
            })
          }
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  // 成员详情
  toUser: function (e) {
    var o = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/group/menberdetail/menberdetail?id=' + o.id,
    })
  },
  // wanshan
  wanshan:function(){
    wx.navigateTo({
      url: '/pages/renzhen/renzhen',
    })
  },
  close:function(){
    this.setData({
      showUser:false,
      success:false,
      iswxzf: false,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})